<template>
  <view class="pending-tasks-stats">
    <view class="section-header">
      <text class="section-title">今日待处理事项</text>
      <text class="section-subtitle">{{ getCurrentDate() }}</text>
    </view>
    <view class="stats-grid">
      <view 
        v-for="(item, index) in taskStats" 
        :key="index" 
        class="stats-item"
        @click="handleTaskClick(item)"
      >
        <view class="stats-number">{{ item.count }}</view>
        <view class="stats-label">{{ item.label }}</view>
        <view :class="['stats-icon', item.iconClass]"></view>
        <view v-if="item.count > 0" class="stats-badge"></view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, defineEmits } from 'vue';
import { getPendingTasksStats } from '@/api';

const emit = defineEmits(['refresh']);

const taskStats = ref([]);
const loading = ref(false);

// 获取当前日期
const getCurrentDate = () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
  const weekDay = weekDays[now.getDay()];
  return `${year}-${month}-${day} 周${weekDay}`;
};

// 获取待处理事项统计数据
const fetchTaskStats = async () => {
  if (loading.value) return;
  
  loading.value = true;
  try {
    // 暂时使用模拟数据，实际项目中调用真实API
    // const res = await getPendingTasksStats();
    // const data = res.data;
    
    const mockData = {
      appeals: Math.floor(Math.random() * 20) + 5,
      workOrders: Math.floor(Math.random() * 15) + 3,
      messages: Math.floor(Math.random() * 25) + 8,
      vehicleVerify: Math.floor(Math.random() * 10) + 2,
      realnameVerify: Math.floor(Math.random() * 8) + 1
    };
    
    taskStats.value = [
      {
        label: '申诉',
        count: mockData.appeals,
        iconClass: 'icon-appeal',
        path: '/pages/order-appeal/index',
        type: 'appeal'
      },
      {
        label: '工单',
        count: mockData.workOrders,
        iconClass: 'icon-workorder',
        path: '/pages/work-order/index',
        type: 'workorder'
      },
      {
        label: '咨询留言',
        count: mockData.messages,
        iconClass: 'icon-message',
        path: '/pages/latest-messages/index',
        type: 'message'
      },
      {
        label: '车辆认证',
        count: mockData.vehicleVerify,
        iconClass: 'icon-verify',
        path: '/pages/vehicle-verify/list/index',
        type: 'vehicle_verify'
      },
      {
        label: '实名认证',
        count: mockData.realnameVerify,
        iconClass: 'icon-realname',
        path: '/pages/realname-verify/list/index',
        type: 'realname_verify'
      }
    ];
    
    // 通知父组件数据已更新
    emit('refresh', {
      total: Object.values(mockData).reduce((sum, count) => sum + count, 0),
      details: mockData
    });
    
  } catch (error) {
    console.error('获取待处理事项统计失败', error);
    uni.showToast({
      title: '获取数据失败',
      icon: 'none'
    });
  } finally {
    loading.value = false;
  }
};

// 处理任务点击
const handleTaskClick = (task) => {
  if (task.count === 0) {
    uni.showToast({
      title: '暂无待处理事项',
      icon: 'none'
    });
    return;
  }
  
  if (task.path) {
    uni.navigateTo({
      url: task.path
    });
  }
};

// 暴露刷新方法给父组件
const refresh = () => {
  fetchTaskStats();
};

// 刷新数据方法（供父组件调用）
const refreshData = async () => {
  await fetchTaskStats();
  emit('refresh');
};

// 暴露方法
defineExpose({
  refresh,
  refreshData
});

// 组件挂载时获取数据
onMounted(() => {
  fetchTaskStats();
});
</script>

<style lang="scss" scoped>
.pending-tasks-stats {
  background-color: #ffffff;
  border-radius: 12rpx;
  padding: 24rpx;
  margin: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
}

.section-subtitle {
  font-size: 24rpx;
  color: #999999;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20rpx;
}

.stats-item {
  position: relative;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12rpx;
  padding: 24rpx;
  text-align: center;
  color: #ffffff;
  overflow: hidden;
  transition: transform 0.2s ease;
}

.stats-item:active {
  transform: scale(0.95);
}

.stats-item:nth-child(1) {
  background: linear-gradient(135deg, #f5222d 0%, #ff7875 100%);
}

.stats-item:nth-child(2) {
  background: linear-gradient(135deg, #722ed1 0%, #b37feb 100%);
}

.stats-item:nth-child(3) {
  background: linear-gradient(135deg, #fa8c16 0%, #ffc069 100%);
}

.stats-item:nth-child(4) {
  background: linear-gradient(135deg, #52c41a 0%, #95de64 100%);
}

.stats-item:nth-child(5) {
  background: linear-gradient(135deg, #1890ff 0%, #69c0ff 100%);
}

.stats-number {
  font-size: 48rpx;
  font-weight: bold;
  margin-bottom: 8rpx;
}

.stats-label {
  font-size: 24rpx;
  opacity: 0.9;
}

.stats-icon {
  position: absolute;
  top: 12rpx;
  right: 12rpx;
  width: 32rpx;
  height: 32rpx;
  opacity: 0.3;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.2);
}

.stats-badge {
  position: absolute;
  top: 8rpx;
  right: 8rpx;
  width: 16rpx;
  height: 16rpx;
  background-color: #ff4d4f;
  border-radius: 50%;
  border: 2rpx solid #ffffff;
}

/* 图标样式 */
.icon-appeal::before {
  content: '申';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 20rpx;
  font-weight: bold;
}

.icon-workorder::before {
  content: '工';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 20rpx;
  font-weight: bold;
}

.icon-message::before {
  content: '咨';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 20rpx;
  font-weight: bold;
}

.icon-verify::before {
  content: '车';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 20rpx;
  font-weight: bold;
}

.icon-realname::before {
  content: '实';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 20rpx;
  font-weight: bold;
}
</style>